<template>
  <Teleport to="body">
    <Transition name="modal">
      <div v-if="show" class="modal-mask" @click="$emit('close')">
        <div class="modal-container" @click.stop>
          <div class="modal-header">
            <h3>所有公告</h3>
            <button class="close-button" @click="$emit('close')">
              <i class="ri-close-line"></i>
            </button>
          </div>
          <div class="modal-body">
            <ul class="announcement-list">
              <li v-for="(item, index) in announcements" :key="index" class="announcement-item">
                <div class="item-icon">
                  <svg width="18" height="18" viewBox="0 0 20 20" fill="none">
                    <circle cx="10" cy="10" r="9" fill="#f4f6fa" />
                    <path
                      d="M10 4.5a3.5 3.5 0 0 0-3.5 3.5v1.1c0 .38-.21.73-.55.91A2.5 2.5 0 0 0 4 12.5V13a1 1 0 0 0 1 1h10a1 1 0 0 0 1-1v-.5a2.5 2.5 0 0 0-1.95-2.49.99.99 0 0 1-.55-.91V8A3.5 3.5 0 0 0 10 4.5Zm0 10.5a2 2 0 0 0 2-2H8a2 2 0 0 0 2 2Z"
                      fill="#3b5bdb"
                    />
                  </svg>
                </div>
                <div class="item-main">
                  <div class="item-title" v-text="item.title"></div>
                  <div class="item-content" v-html="item.content"></div>
                  <div class="item-meta">
                    <span class="meta-author" v-if="item.author" v-text="item.author"></span>
                    <span class="meta-date" v-if="item.date">{{ formatDate(item.date) }}</span>
                  </div>
                </div>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </Transition>
  </Teleport>
</template>

<script setup>
const props = defineProps({
  show: { type: Boolean, required: true },
  announcements: { type: Array, required: true },
})
defineEmits(['close'])

function formatDate(dateStr) {
  if (!dateStr) return ''
  const d = new Date(dateStr)
  if (isNaN(d.getTime())) return dateStr
  const y = d.getFullYear()
  const m = String(d.getMonth() + 1).padStart(2, '0')
  const d2 = String(d.getDate()).padStart(2, '0')
  const hh = String(d.getHours()).padStart(2, '0')
  const mm = String(d.getMinutes()).padStart(2, '0')
  return `${y}-${m}-${d2} ${hh}:${mm}`
}
</script>

<style scoped>
.modal-mask {
  position: fixed;
  z-index: 9998;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  transition: opacity 0.3s ease;
}

.modal-container {
  width: 100%;
  max-width: 520px;
  margin: auto;
  padding: 1.2em 1em 1em 1em;
  background: #fff;
  border-radius: 16px;
  box-shadow:
    0 6px 32px rgba(60, 80, 180, 0.1),
    0 1.5px 8px rgba(0, 0, 0, 0.04);
  position: relative;
  transition: all 0.25s cubic-bezier(0.4, 2, 0.6, 1);
}

@media (max-width: 600px) {
  .modal-container {
    max-width: 95vw;
    padding: 0.7em 0.5em 0.7em 0.5em;
    border-radius: 10px;
  }
  .item-title {
    font-size: 0.97em;
  }
  .item-content {
    font-size: 0.92em;
  }
  .announcement-item {
    padding: 0.6em 0.5em 0.6em 0.5em;
    gap: 0.4em;
  }
}

.modal-header {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 0.7em;
  padding-bottom: 0.5em;
  border-bottom: none;
  position: relative;
}

.modal-header h3 {
  margin: 0 auto;
  font-size: 1.08em;
  color: #222;
  font-weight: 700;
  letter-spacing: 0.01em;
}

.close-button {
  position: absolute;
  right: 0.1em;
  top: 0.1em;
  background: #f7f8fa;
  border: none;
  font-size: 1.1em;
  cursor: pointer;
  color: #888;
  border-radius: 40%;
  width: 1.7em;
  height: 1.7em;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.04);
  transition:
    background 0.18s,
    color 0.18s;
  z-index: 2;
}
.close-button:hover {
  background: #eceffd;
  color: #3b5bdb;
}

.announcement-list {
  list-style: none;
  padding: 0;
  margin: 0;
  max-height: 55vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.7em;
  scrollbar-width: thin;
  scrollbar-color: #e0e7ef #fff;
}

.announcement-list::-webkit-scrollbar {
  width: 6px;
  background: #fff;
}

.announcement-list::-webkit-scrollbar-thumb {
  background: #e0e7ef;
  border-radius: 6px;
}

.announcement-item {
  background: #f8fafd;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(60, 80, 180, 0.06);
  padding: 0.7em 0.9em 0.7em 0.9em;
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  gap: 0.7em;
  transition: box-shadow 0.18s;
}
.announcement-item:hover {
  box-shadow: 0 6px 18px rgba(60, 80, 180, 0.13);
}
.item-icon {
  flex-shrink: 0;
  margin-top: 0.2em;
  margin-left: 0.1em;
}
.item-main {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.1em;
}
.item-title {
  font-weight: 600;
  color: #232946;
  margin: 0 0 0.1em 0;
  font-size: 1em;
  letter-spacing: 0.01em;
  line-height: 1.2;
  word-break: break-all;
}
.item-content {
  color: #444;
  margin: 0 0 0.1em 0;
  line-height: 1.7;
  font-size: 0.97em;
  word-break: break-all;
  white-space: normal;
}
.item-content p {
  margin: 0.2em 0 0.2em 0;
  padding: 0;
  line-height: 1.7;
}
.item-content b,
.item-content strong {
  color: #3b5bdb;
  font-weight: 600;
}
.item-content a {
  color: #2563eb;
  text-decoration: underline;
  transition: color 0.18s;
  word-break: break-all;
}
.item-content a:hover {
  color: #3b5bdb;
}
.item-content span {
  color: #3b5bdb;
}
.item-content br {
  display: block;
  margin-bottom: 0.2em;
  content: '';
}
.item-date {
  font-size: 0.82em;
  color: #b0b4c0;
  text-align: right;
  margin-top: 0.1em;
}

.item-meta {
  display: flex;
  align-items: baseline;
  margin-top: 0.15em;
  color: #9aa1b2;
  font-size: 0.82em;
}
.item-meta .meta-author {
  color: #858ca0;
}
.item-meta .meta-date {
  color: #b0b4c0;
  margin-left: auto;
}

.modal-enter-from,
.modal-leave-to {
  opacity: 0;
}
.modal-enter-from .modal-container,
.modal-leave-to .modal-container {
  transform: scale(0.96) translateY(18px);
  opacity: 0.7;
}
.modal-enter-active .modal-container {
  transition: all 0.25s cubic-bezier(0.4, 2, 0.6, 1);
}
</style>
